	var keyUrl = "120.76.102.10";
	//var keyUrl = "192.168.199.215";
	var re = new RegExp("\\|","g");
	var reg = new RegExp("\\s","g");
	var regX = new RegExp(",","g");
	var msgVlaue;
	var inputSum = 2000;  //核心词返回的结果字节长度
	var inputLength = $("input[class*='inputResult']").length;
	var progressValue = 1;
	var contSum=0;
	var progressfaly = true;
	var progress = 0;
	var stopTime = false;
	var t;
	//var progressTime;
/*获取状态信息*/
function keyStatus(data,stopStatus){
	stopTime = stopStatus;
	var msgDate = "data=" + JSON.stringify(data);
	$.ajax({
			type:"post",
			dataType:"json",
			data:data,
			url:"http://"+keyUrl+"/api/GetSpiderStatus_api.php",
			success:function(msg){
				if(msg.code == 200){
					if(trim(msg.value.Job_ProcessResultValues) == "" && msg.value.Job_ProcessStatus == 200){
						$(".progressMsg").text("关键词无效");
						$(".progress-bar").css("width","0");
						$(".keyStranClick").text("抓一抓");
						return;
					}
					var processIndex = msg.value.Job_ProcessIndex;
					var processStatus = msg.value.Job_ProcessStatus;
					var ProcessCount = msg.value.Job_ProcessCount;
					var procsssNessage = msg.value.Job_ProcessMessage;
					(processIndex == "") ? processIndex = 1 : processIndex = msg.value.Job_ProcessIndex;
					(processStatus == "") ? processStatus = 400 : processStatus = parseInt(msg.value.Job_ProcessStatus);
					(ProcessCount == "") ? ProcessCount = 1 : ProcessCount = msg.value.ProcessCount;
					(procsssNessage == "") ? procsssNessage = "正在抓取中..." : procsssNessage = msg.value.Job_ProcessMessage;
					var sumLength=0,progressLength = 0;
					if(progressfaly){
						var prgessConutLength = parseInt($(".progress-bar").css("width")) / parseInt($(".progress").css("width")) * 100;
							prgessConutLength = Math.round(prgessConutLength);
							
						progressLength = 100-prgessConutLength;
						sumLength = progressLength / ProcessCount;
						contSum = prgessConutLength+sumLength;
						progress = contSum;
						
					}
					if(progressValue == processIndex && processStatus == 400){
						if(stopTime == true) return;
							var prgessConutLength = parseInt($(".progress-bar").css("width")) / parseInt($(".progress").css("width")) * 100;
						 	var randCount = prgessConutLength+(progress / 1500);
						 	$(".progress-bar").css("width",randCount+"%");
							progressfaly = false;
					}else{
						    progressValue = processIndex;
						    progressfaly = true;
						    $(".progress-bar").css("width",progress+"%");
					}
					t = setTimeout("keyStatus("+msgDate+","+stopTime+")",3000);
					$(".progressMsg").text(procsssNessage);
					
					if(processStatus == 200){
						$(".progressMsg").text("已完成");
						$(".progress-bar").css("width","100%");
						getValue(msg);
					}
				}
			},
			error:function(msg){
				alert(msg+"keyajax请求失败");
			}
		});
}	

	function getValue(msg){
		stopTime = true;
		var progressResult = msg.value.Job_ProcessResultValues;
		msgVlaue = progressResult;
		var progressResultLength = progressResult.split("|||").length;
		$(".updatanum").text(progressResultLength);
		var progressArry;
		if(trim($("#productTitle").val()) != ""){
			progressArry = putTitle();
		    progressArry = progressArry.join(",");
		    progressArry = progressArry.replace(regX,"\r\n")
		    
		    var title = putTitle();
			 title = title.join(",");
			 title = title.replace(regX,"|||");
			 msgVlaue = title;
			 $(".updataTextArea").css("line-height","20px");
		}else{
			$(".updataTextArea").css("line-height","10px");
			progressArry = progressResult.replace(/\|/gi,"\r\n");
		}
		
		$(".ctnTextAreatTop textarea").text(progressArry);
		pileBtn();
		falshCtrl();
		
		$(".keyStranClick").text("抓一抓");
		$(".emptyClick").show();
	}
	/*复制按钮的复制功能*/
	function falshCtrl(){
		$(".divDisplay").css("display","block");
		$(".my_clip_button").each(function(i){
	        var id = $(this).attr('data');
	        var clip=null;
	        clip = new ZeroClipboard.Client();
	        ZeroClipboard.setMoviePath( 'js/ZeroClipboard.swf' );  //和html不在同一目录需设置setMoviePath
	        ZeroClipboard.setMoviePath( 'js/ZeroClipboard10.swf' );
	        clip.setHandCursor( true );
	        clip.setText( $(".copy_txt"+id).val() );
	        clip.addEventListener('complete', function (client, text) {
	        });
	        clip.glue( 'copy_btn'+id);
	  });
	}

	/*去掉和标题 一样的单词*/
	function putTitle(){
		var productTitle = $("#productTitle").val();
		var word;
		var arry = [];
		var alls = msgVlaue.split("|||");
		    for(var i=0;i<alls.length;i++){
		    	var isWithout = true;
		    	if(alls[i].indexOf(productTitle) !==-1){
		    		var produArry = alls[alls[i].indexOf(productTitle)];
		    		word = trim(produArry.replace(productTitle,""));
		    		arry.push(word);
		    		isWithout = false;
		    	}
		    	if(isWithout){
					arry.push(alls[i]);
				}
		    }
			
		return arry;
	}
	
	$(".pileBtn").on("click",function(){
		pileBtn();
	})
	
	$(".repeatBtn").on("click",function(){
		repeatBtn();
		if($(".splitCommaBtn").children(".minRadios").children().hasClass("radioLogo")){
			splitComma();
		}else{
			spance();
		}
	})
	
	$(".splitCommaBtn").on("click",function(){
		$(this).addClass("splitCommaB").siblings().removeClass("spanceB");
		if($(".pileBtn").children(".minRadios").children().hasClass("radioLogo")){
			pileAndComma();
		}else{
			splitComma();
		}
		
	})
	
	$(".spanceBtn").on("click",function(){
		$(this).addClass("spanceB").siblings().removeClass("splitCommaB");
		spance();
	})
	
	
	
	/*堆砌*/
	function pileBtn(){
		KeywordsFction();
		if($(".splitCommaBtn").hasClass("splitCommaB")){
			pileAndComma();
		}else{
			spance();
		}
		 
	}
	
	/*去重复*/
	function repeatBtn(){
		for(var k=0;k<inputLength;k++){
			$(".inputResult"+(k+1)).val("");
		}
		var result = msgVlaue.split("|||");
		var _repeat = result.join(",");
		var str = [];
		var alls = [];
		var cc = _repeat.replace(reg,",").split(",");
		for(var i = 0,len = cc.length;i < len;i++){
			if(! RegExp(cc[i],"g").test(str.join(","))){
				str.push(cc[i]);
				alls.push(cc[i]);
			} 
		}
		KeywordsFctions(alls,str);

	}
	
	/*分割成逗号*/
	function splitComma(){
		for(var i=0;i<inputLength;i++){
			if($(".inputResult"+(i+1)).val() == undefined){
				continue;
			}
			var inputResult = $(".inputResult"+(i+1)).val();
			var inputConut = inputResult.replace(reg,",");
			$('.inputResult'+(i+1)).val(inputConut);
		}
	}
	
		/*逗号分割成空格*/
	function spance(){
		for(var i=0;i<inputLength;i++){
			if($(".inputResult"+(i+1)).val() == undefined){
				continue;
			}
			var inputResult = $(".inputResult"+(i+1)).val();
			var inputConut = inputResult.replace(regX," ");
			$('.inputResult'+(i+1)).val(inputConut);
		}
	}
	
	/*堆砌+逗号*/
	function pileAndComma(){
		KeywordsFction();
	}
	
	function KeywordsFction(){
		var arryStringall = msgVlaue.split("|||");
		var alls = msgVlaue.split("|||");
		var conut = 0;
		var index = 0;
		var allFlay = true;
		for(var i=0;i<alls.length;i++){
	    	conut += alls[i].length;
	    	if(conut >=inputSum){
	    		if(arryStringall.length == 0) return;
	    		index++;
	    		$(".inputResult"+index).val(arryStringall.splice(0,(i+1)));
	    		allFlay = false;
	    	}else{
	    		if(allFlay){
	    			$(".inputResult1").val(arryStringall);
	    		}
	    	}
	    }
	}
	function KeywordsFctions(alls,arryStringall){
		var conut = 0;
		var keyindex = 0;
		var allFlay = true;
		for(var i=0;i<alls.length;i++){
	    	conut += alls[i].length;
	    	if(conut >=inputSum){
	    		if(arryStringall.length == 0) return;
	    		keyindex++;
	    		$(".inputResult"+keyindex).val(arryStringall.splice(0,(i+1)));
	    		allFlay = false;
	    	}else{
	    		if(allFlay){
	    			$(".inputResult1").val(arryStringall);
	    		}
	    	}
	    }
	}
	
	function chageSpanBk(index){
		$(".ks"+index).css("background-color","#91b8d0")
	}
	
	/*去掉首位空格 */
	function trim(str){  
	    return str.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'');  
	} 
	
	/*关键词导出功能*/
	$(".submintBtn").on("click",function(){
		var data= {headerTile:["result"],headerbody:[[msgVlaue]]};
			data ="data="+JSON.stringify(data);
		$.ajax({
			type:"post",
			dataType:"json",
			url:"http://"+keyUrl+"/api/cvsexport.php",
			data:data,
			success:function(msg){
				window.location.href = msg.ms;
			},
			error:function(){
				alert("请求失败");
			}
		});
	})
	
	/*利润计算excel导出*/
	$(".tableSubmitBtn").on("click",function(){
		var tableTileArry = [];
		var tableBodyArry = [];
		var  tableArrys = [];
		
		$("#table thead tr th").each(function(){
			tableTileArry.push($(this).text())
		})
		
		$("#table tbody tr td").each(function(){
			tableBodyArry.push($(this).text());
		})
		var len = $("#table tbody tr").length;
		for(var i=0;i<len;i++){
			tableArrys.push(tableBodyArry.splice(i,10));
		}
		tableTileArry.pop();
		var datas= {headerTile:tableTileArry,headerbody:tableArrys};
			datas ="data="+JSON.stringify(datas);
		$.ajax({
			type:"post",
			dataType:"json",
			url:"http://"+keyUrl+"/api/cvsexport.php",
			data:datas,
			success:function(msg){
				window.location.href = msg.ms;
			},
			error:function(){
				alert("请求失败");
			}
		});
	})
	
	
	/*一键调库存*/
	function ckBtn(){
		if($(".ckBtn").children("i").hasClass("okBtn")){
			$(".ckBtn").children("i").removeClass("okBtn glyphicon glyphicon-ok");
		}else{
			$(".ckBtn").children("i").addClass("okBtn glyphicon glyphicon-ok");
		}
	}
